package com.whpost.service.service.stamp;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.config.Wdpz;
import com.whpost.service.dao.stamp.*;
import com.whpost.service.dao.t.TDelDao;
import com.whpost.service.entity.stamp.*;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.t.TDel;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.BaseServices;
import com.whpost.service.service.t.FileService;
import com.whpost.service.service.t.TApiNonceService;
import com.whpost.service.tools.QRCodeGenerator;
import com.whpost.service.tools.ToolBean;
import com.whpost.service.tools.Url;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StampHdServices extends BaseServices {

    @Resource
    private StampXmMapper stampXmMapper;
    @Resource
    private StampHdMapper stampHdMapper;
    @Resource
    private StampHdLxMapper stampHdLxMapper;
    @Resource
    private TApiNonceService tApiNonceService;
    @Resource
    private Wdpz wdpz;
    @Resource
    private FileService fileService;
    @Resource
    private StampCardMapper stampCardMapper;
    @Resource
    private StampHdMbDybMapper stampHdMbDybMapper;
    @Resource
    private TDelDao tDelDao;
    @Resource
    private StampHdMbMapper stampHdMbMapper;

    @Override
    public ResultInfo info(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            TCzy tCzy = new TCzy();
            tCzy = gettCzyDao().selectById(loginId);

            Map<String,Object> infoMap = new HashMap<>();

            String id="";
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            String lx="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }

            if ("1".equals(lx)){
                StampHd stampHd = new StampHd();
                if(StringUtils.isEmpty(id)){
                    return new ResultInfo(500,"修改信息丢失！",null);
                }
                try {
                    id = Url.decrypt(id);
                } catch (Exception e) {
                    throw new RuntimeException("修改信息丢失！");
                }
                stampHd = stampHdMapper.selectById(id);
                if (null!=stampHd){
                    stampHd.setUrlId(ToolBean.getEncode(stampHd.getId()));
                    stampHd.setId(null);
                    stampHd.setUrlXmId(ToolBean.getEncode(stampHd.getXmId()));
                    stampHd.setXmId(null);
                }
                infoMap.put("stampHd",stampHd);
            }

            //类型
            List<StampHdLx> stampHdLxList = new ArrayList<>();
            stampHdLxList = stampHdLxMapper.selectList(null);


            //项目筛选，暂时规定，只能选择自己机构的项目
            List<StampXm> stampXmList = stampXmMapper.selectList(new QueryWrapper<StampXm>().eq("t_xzjg_id",tCzy.getXzjgId()).eq("state","1"));
            // 循环遍历 List 并改变每个对象的 age 属性
            for (StampXm stampXm : stampXmList) {
                stampXm.setUrlId(ToolBean.getEncode(stampXm.getId()));
                stampXm.setId(null);
            }
            infoMap.put("stampXmList",stampXmList);
            infoMap.put("stampHdLxList",stampHdLxList);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    public ResultInfo query(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String curpage = "",pagesize="",cxmc="",xmxx_id="";
            TCzy tCzy = gettCzyDao().selectById(loginId);
            if (jsonObject.containsKey("curpage")) {
                curpage = jsonObject.getString("curpage");
            }
            if (jsonObject.containsKey("pagesize")) {
                pagesize = jsonObject.getString("pagesize");
            }
            if (jsonObject.containsKey("cxmc")) {
                cxmc = jsonObject.getString("cxmc");
            }
            if (jsonObject.containsKey("xmxx_id")) {
                xmxx_id = jsonObject.getString("xmxx_id");
            }

            if (StringUtils.isEmpty(curpage)) curpage = "1";
            if (StringUtils.isEmpty(pagesize)) pagesize = "10";

            QueryWrapper<StampHd> queryWrapper = new QueryWrapper<>();
            Page<StampHd> page = new Page<>( Integer.parseInt(curpage),  Integer.parseInt(pagesize));

            ListInfo<StampHd> stampHdListInfo = new ListInfo<>();
            if (!StringUtils.isEmpty(cxmc))
            {
                queryWrapper.like("name",cxmc);
            }
            if (!StringUtils.isEmpty(xmxx_id))
            {
                queryWrapper.eq("xm_id",ToolBean.getDecodeWithNetDecode(xmxx_id));
            }
            queryWrapper.eq("t_xzjg_id",tCzy.getXzjgId());
            queryWrapper.orderByDesc("id");

            IPage<StampHd> stampHdIPage = stampHdMapper.getHdIPage(page,queryWrapper);

            PageInfo pageInfo = new PageInfo();
            pageInfo.setTotal(stampHdIPage.getTotal());
            pageInfo.setCurPage(stampHdIPage.getCurrent());
            pageInfo.setPageSize(stampHdIPage.getSize());
            pageInfo.setPageTotal(stampHdIPage.getPages());

            stampHdListInfo.setPageInfo(pageInfo);

            List<StampHd> stampHdList = stampHdIPage.getRecords();

            // 循环遍历 List 并改变每个对象的 age 属性
            for (StampHd stampHd : stampHdList) {
                stampHd.setUrlId(ToolBean.getEncode(stampHd.getId()));
                stampHd.setId(null);
            }

            stampHdListInfo.setListInfo(stampHdList);
            stampHdListInfo.setUserId(loginId);

            //查询条件的值
            Map<String,Object> cxtjMap = new HashMap<>();
            cxtjMap.put("cxmc",cxmc);
            cxtjMap.put("xmxx_id",xmxx_id);

            //项目筛选，暂时规定，只能选择自己机构的项目
            List<StampXm> stampXmList = stampXmMapper.selectList(new QueryWrapper<StampXm>().eq("t_xzjg_id",tCzy.getXzjgId()).eq("state","1"));
            // 循环遍历 List 并改变每个对象的 age 属性
            for (StampXm stampXm : stampXmList) {
                stampXm.setUrlId(ToolBean.getEncode(stampXm.getId()));
                stampXm.setId(null);
            }
            cxtjMap.put("stampXmList",stampXmList);

            //返回前端的内容
            Map<String,Object> rtnMap = new HashMap<>();
            rtnMap.put("cxjg",stampHdListInfo);
            rtnMap.put("cxtj",cxtjMap);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",rtnMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo mbquery(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String curpage = "",pagesize="",hd_id="",mblx="";
            TCzy tCzy = gettCzyDao().selectById(loginId);
            if (jsonObject.containsKey("curpage")) {
                curpage = jsonObject.getString("curpage");
            }
            if (jsonObject.containsKey("pagesize")) {
                pagesize = jsonObject.getString("pagesize");
            }
            if (jsonObject.containsKey("hd_id")) {
                hd_id = jsonObject.getString("hd_id");
            }
            if (jsonObject.containsKey("mblx")) {
                mblx = jsonObject.getString("mblx");
            }
            if (!StringUtils.isEmpty(hd_id)){
                try {
                    hd_id = Url.decrypt(hd_id);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            if (StringUtils.isEmpty(curpage)) curpage = "1";
            if (StringUtils.isEmpty(pagesize)) pagesize = "10";

            IPage<StampHdMbDyb> stampHdMbDybIPage =null;
            QueryWrapper<StampHdMbDyb> queryWrapper = new QueryWrapper<StampHdMbDyb>();
            Page<StampHdMbDyb> page = new Page<StampHdMbDyb>(Integer.parseInt(curpage),10);

            if (!StringUtils.isEmpty(mblx))
            {
                queryWrapper.eq("lx",mblx);
            }
            queryWrapper.eq("hd_id",hd_id);
            stampHdMbDybIPage =stampHdMbDybMapper.selectPage(page,queryWrapper);



            ListInfo<StampHdMbDyb> stampHdMbDybListInfo = new ListInfo<>();

            PageInfo pageInfo = new PageInfo();
            pageInfo.setTotal(stampHdMbDybIPage.getTotal());
            pageInfo.setCurPage(stampHdMbDybIPage.getCurrent());
            pageInfo.setPageSize(stampHdMbDybIPage.getSize());
            pageInfo.setPageTotal(stampHdMbDybIPage.getPages());

            stampHdMbDybListInfo.setPageInfo(pageInfo);


            List<StampHdMbDyb> stampHdMbDybList = stampHdMbDybIPage.getRecords();
            // 循环遍历 List 并改变每个对象的 age 属性
            for (StampHdMbDyb stampHdMbDyb : stampHdMbDybList) {
                stampHdMbDyb.setMbUrlId(ToolBean.getEncode(stampHdMbDyb.getMbId()));
            }
            stampHdMbDybListInfo.setListInfo(stampHdMbDybList);
            stampHdMbDybListInfo.setUserId(loginId);

            //查询条件的值
            Map<String,Object> cxtjMap = new HashMap<>();
            cxtjMap.put("mblx",mblx);
            String jm = ToolBean.getEncode(hd_id);
            cxtjMap.put("hd_id",jm);

            //返回前端的内容
            Map<String,Object> rtnMap = new HashMap<>();
            rtnMap.put("cxjg",stampHdMbDybListInfo);
            rtnMap.put("cxtj",cxtjMap);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",rtnMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    @Transactional
    public ResultInfo save(String loginId, MultipartFile file, MultipartFile file1,MultipartFile file2,MultipartFile file3,MultipartFile file4,MultipartFile file5,MultipartFile file6,String data) {
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            JSONObject jsonObject = getDataStr(data,loginId);
            String lx="",id="",nonce_ms="",nonce_zhi="",hdmc="",xm_id="",lx_id="",sfsyxs="",jg="",yf="",hdzt="",xsxh="",zdxdsl="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("hdmc")) {
                hdmc = jsonObject.getString("hdmc");
            }
            if (jsonObject.containsKey("xm_id")) {
                xm_id = jsonObject.getString("xm_id");
            }
            if (jsonObject.containsKey("lx_id")) {
                lx_id = jsonObject.getString("lx_id");
            }
            if (jsonObject.containsKey("sfsyxs")) {
                sfsyxs = jsonObject.getString("sfsyxs");
            }
            if (jsonObject.containsKey("jg")) {
                jg = jsonObject.getString("jg");
            }
            if (jsonObject.containsKey("yf")) {
                yf = jsonObject.getString("yf");
            }
            if (jsonObject.containsKey("hdzt")) {
                hdzt = jsonObject.getString("hdzt");
            }
            if (jsonObject.containsKey("xsxh")) {
                xsxh = jsonObject.getString("xsxh");
            }
            if (jsonObject.containsKey("zdxdsl")) {
                zdxdsl = jsonObject.getString("zdxdsl");
            }
            if (StringUtils.isEmpty(lx) || (StringUtils.isEmpty(id) && "1".equals(lx))){
                return new ResultInfo(500,"关键参数信息丢失！",null);
            }
            if (StringUtils.isEmpty(hdmc) || StringUtils.isEmpty(xm_id) || StringUtils.isEmpty(lx_id) || StringUtils.isEmpty(jg) || StringUtils.isEmpty(yf) || StringUtils.isEmpty(xsxh) || StringUtils.isEmpty(hdzt) || StringUtils.isEmpty(sfsyxs)){
                return new ResultInfo(500,"录入信息丢失！",null);
            }

            if (jsonObject.containsKey("nonce_ms")) {
                nonce_ms = jsonObject.getString("nonce_ms");
            }
            if (jsonObject.containsKey("nonce_zhi")) {
                nonce_zhi = jsonObject.getString("nonce_zhi");
            }

            //校验nonce
            ResultInfo ResultInfoCheckNonce = new ResultInfo();
            ResultInfoCheckNonce = tApiNonceService.checkNonce(nonce_ms,loginId,nonce_zhi,"stampxm.save");

            if (ResultInfoCheckNonce.getCode()!=200)
            {
                return ResultInfoCheckNonce;
            }

            TCzy tCzy = gettCzyDao().getCzyById(loginId);

            StampHd stampHd = new StampHd();
            if ("1".equals(lx)){
                if(StringUtils.isEmpty(id)){
                    return new ResultInfo(500,"修改信息丢失！",null);
                }
                try {
                    id = Url.decrypt(id);
                } catch (Exception e) {
                    throw new RuntimeException("修改信息丢失！");
                }
                stampHd = stampHdMapper.selectById(id);

                if(!stampHd.getTXzjgId().equals(tCzy.getXzjgId())){
                    return new ResultInfo(500,"只支持自己修改自己机构建立的活动！",null);
                }

            }

            stampHd.setName(hdmc);
            stampHd.setXmId(Long.parseLong(ToolBean.getDecodeWithNetDecode(xm_id)));
            stampHd.setLxId(Long.parseLong(lx_id));
            stampHd.setZjm(ToolBean.getPy(hdmc));
            stampHd.setTCzyId(tCzy.getId());
            stampHd.setTXzjgId(tCzy.getXzjgId());

            stampHd.setJg(Double.parseDouble(jg));
            stampHd.setYf(Double.parseDouble(yf));
            stampHd.setState(hdzt);
            stampHd.setXsxh(Integer.parseInt(xsxh));
            stampHd.setZdxdsl(Integer.parseInt(zdxdsl));
            stampHd.setSfsyxs(sfsyxs);

            if ("1".equals(lx)){
                //修改的时候，文件上传变更
                String lj = "";
                String os = System.getProperty("os.name");
                if (os != null && os.contains("Windows")) {
                    lj = "d:\\stamp\\hd\\" + stampHd.getId()+"\\";
                } else {
                    //linux
                    lj = "/home/stamp/hd/"+stampHd.getId()+"/";
                }
                //上传文件  0 展示图片  1 背景  2 横版相框  3 竖版相框 4 个性化相框 5 定位图 6 个性化模版图片
                if (null!=file){
                    fileService.uploadFileToLj(file,lj,stampHd.getId() + "_zstp.png");
                }
                if (null!=file1){
                    fileService.uploadFileToLj(file1,lj,stampHd.getId() + "_bjtp.png");
                }
                if (null!=file2){
                    fileService.uploadFileToLj(file2,lj,stampHd.getId() + "_hbxk.png");
                }
                if (null!=file3){
                    fileService.uploadFileToLj(file3,lj,stampHd.getId() + "_sbxk.png");
                }
                if (null!=file4){
                    fileService.uploadFileToLj(file4,lj,stampHd.getId() + "_gxhxk.png");
                }
                if (null!=file6){
                    fileService.uploadFileToLj(file6,lj,stampHd.getId() + "_gxhbj.png");
                    fileService.uploadFileToLjSf(lj,stampHd.getId() + "_gxhbj.png",stampHd.getId() + "_gxhbj_slt.png");
                }
                if (null!=file5){
                    fileService.uploadFileToLj(file5,lj,stampHd.getId() + "_dwt.png");
                }
                stampHd.setHbxk(stampHd.getId() + "_hbxk.png");
                stampHd.setSbxk(stampHd.getId() + "_sbxk.png");
                stampHd.setDwt(stampHd.getId() + "_dwt.png");
                stampHd.setGxhMb(stampHd.getId() + "_gxhbj.png");
                stampHd.setGxhMbSlt(stampHd.getId() + "_gxhbj_slt.png");
                stampHd.setBjtp(stampHd.getId() + "_zstp.png");
                stampHd.setGxhxk(stampHd.getId() + "_gxhxk.png");
                stampHd.setBjtp1(stampHd.getId() + "_bjtp.png");
                stampHdMapper.updateById(stampHd);
                return new ResultInfo(0,"操作成功！",null);
            }else{
                stampHdMapper.insert(stampHd);
                stampHd.setHbxk(stampHd.getId() + "_hbxk.png");
                stampHd.setSbxk(stampHd.getId() + "_sbxk.png");
                stampHd.setDwt(stampHd.getId() + "_dwt.png");
                stampHd.setGxhMb(stampHd.getId() + "_gxhbj.png");
                stampHd.setGxhMbSlt(stampHd.getId() + "_gxhbj_slt.png");
                stampHd.setBjtp(stampHd.getId() + "_zstp.png");
                stampHd.setGxhxk(stampHd.getId() + "_gxhxk.png");
                stampHd.setBjtp1(stampHd.getId() + "_bjtp.png");
                String lj = "";
                String os = System.getProperty("os.name");
                if (os != null && os.contains("Windows")) {
                    lj = "d:\\stamp\\hd\\" + stampHd.getId()+"\\";
                } else {
                    //linux
                    lj = "/home/stamp/hd/"+stampHd.getId()+"/";
                }
                //上传文件  0 展示图片  1 背景  2 横版相框  3 竖版相框 4 个性化相框 5 定位图 6 个性化模版图片
                if (null!=file){
                    fileService.uploadFileToLj(file,lj,stampHd.getId() + "_zstp.png");
                }
                if (null!=file1){
                    fileService.uploadFileToLj(file1,lj,stampHd.getId() + "_bjtp.png");
                }
                if (null!=file2){
                    fileService.uploadFileToLj(file2,lj,stampHd.getId() + "_hbxk.png");
                }
                if (null!=file3){
                    fileService.uploadFileToLj(file3,lj,stampHd.getId() + "_sbxk.png");
                }
                if (null!=file4){
                    fileService.uploadFileToLj(file4,lj,stampHd.getId() + "_gxhxk.png");
                }
                if (null!=file6){
                    fileService.uploadFileToLj(file6,lj,stampHd.getId() + "_gxhbj.png");
                    fileService.uploadFileToLjSf(lj,stampHd.getId() + "_gxhbj.png",stampHd.getId() + "_gxhbj_slt.png");
                }
                if (null!=file5){
                    fileService.uploadFileToLj(file5,lj,stampHd.getId() + "_dwt.png");
                }
                stampHdMapper.updateById(stampHd);
                return new ResultInfo(0,"操作成功！",null);
            }
        }
    }

    /**
     * 这个是保存活动模版的逻辑
     * @param loginId
     * @param file
     * @param data
     * @return
     */
    @Override
    @Transactional
    public ResultInfo save(String loginId, List<MultipartFile> file, String data) {
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            JSONObject jsonObject = getDataStr(data,loginId);
            TCzy tCzy = gettCzyDao().selectById(loginId);
            String hd_id="",nonce_ms="",nonce_zhi="",mblx="";
            if (jsonObject.containsKey("id")) {
                hd_id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("mblx")) {
                mblx = jsonObject.getString("mblx");
            }
            if (StringUtils.isEmpty(mblx) || StringUtils.isEmpty(hd_id)){
                return new ResultInfo(500,"关键参数信息丢失！",null);
            }
            ToolBean.info("hd_id:"+hd_id);
            hd_id = ToolBean.getDecode(hd_id);
            ToolBean.info("hd_id解密后:"+hd_id);
            if (StringUtils.isEmpty(hd_id)){
                return new ResultInfo(500,"活动信息丢失！",null);
            }

            StampHd stampHd = new StampHd();
            stampHd = stampHdMapper.selectById(hd_id);

            //暂时不做限制，都可以上传模版

            if (jsonObject.containsKey("nonce_ms")) {
                nonce_ms = jsonObject.getString("nonce_ms");
            }
            if (jsonObject.containsKey("nonce_zhi")) {
                nonce_zhi = jsonObject.getString("nonce_zhi");
            }

            //校验nonce
            ResultInfo ResultInfoCheckNonce = new ResultInfo();
            ResultInfoCheckNonce = tApiNonceService.checkNonce(nonce_ms,loginId,nonce_zhi,"stampmb.save");

            if (ResultInfoCheckNonce.getCode()!=200)
            {
                return ResultInfoCheckNonce;
            }

            String os = System.getProperty("os.name");
            String picPathReal = "";
            if (os != null && os.contains("Windows")) {
                //System.out.println("Windows");
                picPathReal = "d:\\stamp\\mb\\"+mblx+"\\";
            } else {
                //linux
                picPathReal = "/home/stamp/mb/"+mblx+"/";
            }
            StampHdMb stampHdMb = new StampHdMb();
            StampHdMbDyb stampHdMbDyb = new StampHdMbDyb();

            if (null!=file){
                for (MultipartFile wj : file) {
                    if (StringUtils.isEmpty(wj.getOriginalFilename())){
                        continue;
                    }
                    //保存模版
                    stampHdMb = new StampHdMb();
                    stampHdMb.setName("");
                    stampHdMb.setLx(mblx);
                    stampHdMb.setTCzyId(tCzy.getId());
                    stampHdMb.setTXzjgId(tCzy.getXzjgId());
                    stampHdMbMapper.insert(stampHdMb);

                    String wjm = String.valueOf(stampHdMb.getId()) + ".png";
                    String wjm_slt = String.valueOf(stampHdMb.getId()) + "_slt.png";

                    stampHdMb.setMblj(wjm);
                    stampHdMb.setMblj1(wjm_slt);
                    stampHdMbMapper.updateById(stampHdMb);

                    stampHdMbDyb.setHdId(Long.parseLong(hd_id));
                    stampHdMbDyb.setLx(mblx);
                    stampHdMbDyb.setMbId(stampHdMb.getId());
                    stampHdMbDyb.setTCzyId(Long.parseLong(loginId));
                    stampHdMbDyb.setAddRq(ToolBean.getToday());
                    stampHdMbDybMapper.insert(stampHdMbDyb);

                    fileService.uploadFileSlt(wj, picPathReal, wjm,wjm_slt,mblx);
                }
            }

            return new ResultInfo(0,"操作成功！",null);

        }
    }

    @Override
    @Transactional
    public ResultInfo del(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String ids = "";
            if (jsonObject.containsKey("ids")) {
                ids = jsonObject.getString("ids");
            }
            String [] code = ids.split(",");
            int i=0;
            String str="";

            TCzy tCzy = gettCzyDao().getCzyById(loginId);
            StampHd stampHd;

            for(i=0;i<code.length;i++)
            {
                if (code[i].equals("")) {
                    continue;
                }
                String id = null;
                try {
                    ToolBean.info("code[i]准备解密："+code[i]);
                    id = Url.decrypt(java.net.URLDecoder.decode(code[i],"utf-8"));
                    ToolBean.info("code[i]准备解密后："+id);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException("");
                }

                stampHd = stampHdMapper.selectById(id);

                //项目经理和操作员职工信息一致的，才能删除！
                if (!stampHd.getTCzyId().equals(tCzy.getId())){
                    str+=stampHd.getName()+"[非本人建立的活动，无法删除]，";
                    continue;
                }

                //如果项目没有任何动态信息，直接连软件信息一起删除！
                long sl1 = stampCardMapper.selectCount(new QueryWrapper<StampCard>().eq("hd_id",id));;
                if (sl1>0) {
                    str+=stampHd.getName()+"，";
                    continue;
                }
                String lj="";
                try {
                    String os = System.getProperty("os.name");
                    lj="";
                    if (os != null && os.contains("Windows")) {
                        lj = "d:\\stamp\\hd\\" + stampHd.getId()+"\\";
                    } else {
                        //linux
                        lj = "/home/stamp/hd/"+stampHd.getId()+"/";
                    }

                    TDel tDel = new TDel();
                    tDel.setLj(lj);
                    tDel.setLx("2");
                    tDelDao.insert(tDel);

                    File directory = new File(lj);
                    if (directory.exists()){
                        File[] files = directory.listFiles();
                        if (files != null) {
                            for (File file : files) {
                                file.delete();
                            }
                        }
                        // 删除空文件夹
                        directory.delete();
                    }
                    //删除逻辑，注意不要用错了mapper
                    stampHdMapper.deleteById(stampHd);
                    stampHdMbDybMapper.delete(new QueryWrapper<StampHdMbDyb>().eq("hd_id",id));
                }catch (Exception e){
                    ToolBean.info("StampHdServices.del文件目录["+lj+"]删除发生错误："+e.toString());
                }
            }
            ResultInfo resultInfo;
            if (str.length()>0) {
                str = str.substring(0,str.length()-1);
            }
            if (!StringUtils.isEmpty(str))
            {
                str="["+str+"]活动下有用户痕迹，无法删除！";
                resultInfo = new ResultInfo(500, str,null);
            }
            else {
                resultInfo = new ResultInfo(0, str,null);
            }
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo getHdYl(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String hd_id="",lx="";
            if (jsonObject.containsKey("hd_id")) {
                hd_id = jsonObject.getString("hd_id");
            }
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if(StringUtils.isEmpty(hd_id)){
                return new ResultInfo(500,"活动信息丢失！",null);
            }
            try {
                hd_id = Url.decrypt(hd_id);
            } catch (Exception e) {
                throw new RuntimeException("活动信息丢失！");
            }
            StampHd stampHd = stampHdMapper.selectById(hd_id);

            if (null!=stampHd){
                stampHd.setUrlId(ToolBean.getEncode(stampHd.getId()));
                stampHd.setId(null);
            }

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",stampHd);
            return backRtn(resultInfo,loginId);
        }
    }

    @Transactional
    public ResultInfo mbdel(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String ids = "";
            if (jsonObject.containsKey("ids")) {
                ids = jsonObject.getString("ids");
            }
            String [] code = ids.split(",");
            int i=0;
            String str="";

            TCzy tCzy = gettCzyDao().getCzyById(loginId);
            StampHdMbDyb stampHdMbDyb;

            for(i=0;i<code.length;i++)
            {
                if (code[i].equals("")) {
                    continue;
                }
                String id = null;
                try {
                    ToolBean.info("code[i]准备解密："+code[i]);
                    id = code[i];
                    ToolBean.info("code[i]准备解密后："+id);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException("");
                }

                stampHdMbDyb = stampHdMbDybMapper.selectById(id);

                if (null==stampHdMbDyb.getTCzyId()){
                    str+=stampHdMbDyb.getHdId()+"[无法确定模版上传人，无法删除]，";
                    continue;
                }

                //项目经理和操作员职工信息一致的，才能删除！
                if (!stampHdMbDyb.getTCzyId().equals(tCzy.getId())){
                    str+=stampHdMbDyb.getHdId()+"[非本人上传的模版，无法删除]，";
                    continue;
                }
                stampHdMbDybMapper.deleteById(stampHdMbDyb);

            }
            ResultInfo resultInfo;
            if (str.length()>0) {
                str = str.substring(0,str.length()-1);
            }
            if (!StringUtils.isEmpty(str))
            {
                str="["+str+"]！无法删除！";
                resultInfo = new ResultInfo(500, str,null);
            }
            else {
                resultInfo = new ResultInfo(0, str,null);
            }
            return backRtn(resultInfo,loginId);
        }
    }
}
